package com.linxianqin.springbootVue.controller.monitor;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxianqin.springbootVue.domain.convert.SysLogininforConvert;
import com.linxianqin.springbootVue.domain.dto.SysLogininforDTO;
import com.linxianqin.springbootVue.domain.entity.SysLogininfor;
import com.linxianqin.springbootVue.domain.enums.BusinessType;
import com.linxianqin.springbootVue.domain.model.AjaxResult;
import com.linxianqin.springbootVue.domain.page.TableDataInfo;
import com.linxianqin.springbootVue.domain.query.SysLogininforQuery;
import com.linxianqin.springbootVue.domain.vo.SysLogininforExcel;
import com.linxianqin.springbootVue.manage.annotation.SysLog;
import com.linxianqin.springbootVue.manage.core.BaseController;
import com.linxianqin.springbootVue.service.SysLogininforService;
import com.linxianqin.springbootVue.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能描述：系统访问记录
 * @author lxq
 * @version 1.00
 * @Date 2020/12/14
 */
@Slf4j
@Api(value = "系统访问记录")
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController {

    private final SysLogininforService logininforService;
    public SysLogininforController(SysLogininforService logininforService){
        this.logininforService=logininforService;
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLogininforQuery query)
    {
        log.info("---------系统访问记录---------------");
        //构建分页bean
        Page<SysLogininforDTO> page = buildPage();
        //查询分页数据
        Page<SysLogininforDTO> dtoPage = logininforService.selectLogininforPage(page, query);
        return getDataTable(dtoPage);
    }

//    @SysLog(title = "登录日志", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
//    @GetMapping("/export")
//    public AjaxResult export(SysLogininforDTO logininforDTO)
//    {
//        log.info("===================日志导出===================");
//        //查询数据库，查询所有
//        List<SysLogininfor> list = logininforService.list();
//        //构建Excel工具包
//        ExcelUtil<SysLogininforExcel> util = new ExcelUtil<>(SysLogininforExcel.class);
//        //转化实体
//        List<SysLogininforExcel> sysLogininforExcels = SysLogininforConvert.INSTANCE.entity2Excel(list);
//        //序号id
//        for (int i = 0; i < sysLogininforExcels.size(); i++) {
//            sysLogininforExcels.get(i).setIndex(i+1);
//        }
//        return util.exportExcel(sysLogininforExcels, "登录日志");
//    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @SysLog(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable List<Long>infoIds)
    {
        log.info("打印删除数据ids:{}",infoIds);
        return toAjax(logininforService.removeByIds(infoIds));
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @SysLog(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        log.info("===================清空日志===================");
        logininforService.cleanLogininfor();
        return AjaxResult.success();
    }
}
